package cameratricks;

import java.awt.Rectangle;

import processing.core.PApplet;
import processing.core.PImage;
import processing.video.Capture;
import hypermedia.video.Blob;
import hypermedia.video.OpenCV;

public class BlobConnector implements Renderer {
	Capture capture;
	PApplet p;

	public BlobConnector(TrickCarousel p) {
		this.capture = p.getCapture();
		this.p = p;
	}

	public void draw(OpenCV opencv) {
		PImage image = opencv.image();

		opencv.threshold(50, 150, OpenCV.THRESH_OTSU); // set black & white
														// threshold

		Blob[] blobs = opencv.blobs(60, image.width * image.height / 2, 200,
				true, OpenCV.MAX_VERTICES * 4);

		image.filter(p.GRAY);
		p.image(image, 0, 0);

		for (Blob b : blobs) {
			for (int i = 0; i < blobs.length; i++) {
				
				pickRandomFill();
				
				p.beginShape();
				for (int j = 0; j < blobs[i].points.length; j++) {

					int blobx = blobs[i].points[j].x;
					int bloby = blobs[i].points[j].y;
					p.vertex(blobx, bloby);
				}
				p.endShape(p.CLOSE);
			}

		}

	}

	private void pickRandomFill() {
		// TODO Auto-generated method stub
		float random = p.random(1F);
		if (random < 0.3F)
			p.fill(250,0,0,90);
		else if (random < 0.8F && random  > 0.3F)
			p.fill(0,250,0,90);
		else
			p.fill(0,0,255,90);
				
	}

}
